//
//  BLBadgeButton.m
//  新浪微博
//
//  Created by hbl on 16/2/14.
//  Copyright © 2016年 hbl. All rights reserved.
//

#import "BLBadgeButton.h"

@implementation BLBadgeButton

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.hidden = YES;
        self.userInteractionEnabled = NO;
        UIImage *background = [UIImage imageNamed:@"main_badge_os7"];
        background = [background resizableImageWithCapInsets:UIEdgeInsetsMake(background.size.height * 0.5, background.size.width * 0.5,background.size.height * 0.5, background.size.width * 0.5)];
        [self setBackgroundImage:background forState:UIControlStateNormal];
        self.titleLabel.font = [UIFont systemFontOfSize:11.0];
    }
    return self;
}

- (void)setBadgeValue:(NSString *)badgeValue
{
    NSDictionary *attr =@{NSFontAttributeName:[UIFont systemFontOfSize:11.0]};
    CGRect titleRect = [badgeValue boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil];
    
    if (badgeValue) {
        [self setTitle:badgeValue forState:UIControlStateNormal];
        CGFloat badgeH = self.currentBackgroundImage.size.height;
        CGFloat badgeW = self.currentBackgroundImage.size.width;
        if (badgeValue.length > 1) {
            badgeW = titleRect.size.width + 10;
        }
        CGRect frame = self.frame;
        frame.size.height = badgeH;
        frame.size.width = badgeW;
        self.frame = frame;
        self.hidden = NO;
    }
}

@end
